/*
 * ClockGUI.java
 */

/* Copyright information:
 * 
 *  Copyright 2010 Failboat Productions. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY FAILBOAT PRODUCTIONS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FAILBOAT PRODUCTIONS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Failboat Productions.
 */

package GUI;

import pieces.Piece.ChessColor;

import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.gui.QLCDNumber;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

/**
 * A class to handle timer features that cannot be saved/serialized
 * @author Failboat
 *
 */
class ClockGUI extends QWidget{
	
	private QLCDNumber lcd;
    private QTimer timer = new QTimer();
    private ClockCore playTimer;
	private ChessGUI gui;
	private ChessColor color;
	
    /* Constructor */
	ClockGUI(ChessGUI gui, ChessColor color){
		this.color = color;
		this.gui = gui;
        lcd = new QLCDNumber(5);
        lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Flat);
        playTimer = new ClockCore();
        QVBoxLayout layout = new QVBoxLayout();
        layout.addWidget(lcd);
        setLayout(layout);
        if(playTimer.isCountdown()&&!playTimer.isQuick()){
        	lcd.display("90:00");
        }else if(playTimer.isCountdown()&&playTimer.isQuick()){
        	lcd.display("10:00");
        }else{
        	lcd.display("00:00");
        }
        timer.timeout.connect(this, "addSecond()");
        timer.start(1000);
	}
	
	/**
	 * Method that calls addSecond in playTimer and refreshes the view on the clock
	 */
	void addSecond(){
		playTimer.addSecond();
		if(playTimer.isCountdown()&&playTimer.getTime()==0){
			gui.endGame(color);
		}
		refreshClock();
	}
	
	/**
	 * Method to get the PlayTimer class, for saving it
	 * @return playTimer
	 * (PlayTimer)
	 */
	ClockCore getTimer(){
		return playTimer;
	}
	
	/**
	 * Method to set the PlayTimer class, for loading it from a savegame
	 * @param timer
	 *(PlayTimer)
	 */
	void setTimer(ClockCore playTimer){
		this.playTimer = playTimer;
	}
	
	/**
	 * Method to take care of start and stop of the timer and send the parameters to playTimer
	 * @param start
	 * (boolean)
	 * @param turnChanged
	 * (boolean)
	 */
	void runTimer(boolean start, boolean turnChanged){
		 playTimer.runTimer(start, turnChanged);
	    	if(start){
	        	timer.start();
	        }else{
	        	timer.stop();
	        }
	 }
	 
	/**
	 * Method to refresh the clock after increasing with 1 second or loading a game
	 */
	void refreshClock(){
		 lcd.display(playTimer.clockString());
	 }
}
